<template>
    <div>
        <el-form :inline="true" :model="user" class="demo-form-inline">
  <el-form-item label="用户名">
    <el-input v-model="user.username" placeholder="用户名"></el-input>
  </el-form-item>
  <el-form-item label="电话">
    <el-input v-model="user.phone" placeholder="电话号码"></el-input>
  </el-form-item>
  <el-form-item label="创建时间">
     <el-col :span="11">
      <el-date-picker type="date" format="yyyy-MM-dd" placeholder="选择日期" v-model="user.minCreateTime" style="width: 100%;"></el-date-picker>
    </el-col>
    <el-col class="line" :span="2">-</el-col>
    <el-col :span="11">
      <el-date-picker type="date" format="yyyy-MM-dd" placeholder="选择日期" v-model="user.maxCreateTime" style="width: 100%;"></el-date-picker>
    </el-col>
  </el-form-item>
  <el-form-item label="权限">
    <el-select v-model="user.level" placeholder="请选择权限">
      <el-option label="普通用户" value="1"></el-option>
      <el-option label="管理员" value="2"></el-option>
    </el-select>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="condition()">查询</el-button>
    
    <el-button icon="el-icon-delete" type="primary" @click="user={}">清空</el-button>
  </el-form-item>
</el-form>
      <!-- 表格   -->
    <el-table
    :data="page.records"
    border
    style="width: 85%">
        <el-table-column
        prop="id"
        label="用户ID"
        width="100">
        </el-table-column>
        <el-table-column
        prop="username"
        label="姓名"
        width="180">
        </el-table-column>
        <el-table-column
        prop="phone"
        label="电话"
         width="180">
        </el-table-column>
        <el-table-column
        prop="level"
        label="权限"
         width="180">
        <template slot-scope="scope">
            <el-tag v-if="scope.row.level==1">普通用户</el-tag>
            <el-tag type="danger" v-if="scope.row.level==2">管理员</el-tag>
        </template>
        </el-table-column>
        <el-table-column
        prop="createTime"
        label="创建时间"
         width="180">
        </el-table-column>

         <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button
          size="mini"
          v-if="scope.row.level==1"
          @click="levelUp(scope.row.id)">升为管理员</el-button>
          <el-button
          size="mini"
          type="warning"
          v-if="scope.row.level==2"
          @click="levelDown(scope.row.id)">降级为普通用户</el-button>
        <el-button
          size="mini"
          type="danger"
          v-if="scope.row.state==1"
          @click="ban(scope.row.id)">封禁</el-button>
          <el-button
          size="mini"
          type="success"
          v-if="scope.row.state==2"
          @click="unban(scope.row.id)">解封</el-button>
      </template>
    </el-table-column>
  </el-table>

  <!-- 分页 -->
  <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.current"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total">
    </el-pagination>
    </div>
</template>

<script>
import {condition,levelUp,levelDown,ban,unban} from '@/api/user';
export default {
    data() {
        return {
            user:{
                current:null,
                size:null
            },
            page:{
                current:1,
                size:10,
                total:0
            }
        }
    },
    methods: {
        async condition(){
            this.user.current = this.page.current;
            this.user.size = this.page.size;
            let {data} = await condition(this.user);
            this.page = data
        },
        async levelUp(id){
            let {message} = await levelUp(id)
            this.$message.success(message)
            this.condition()
        },
        async levelDown(id){
            let {message} = await levelDown(id)
            this.$message.success(message)
            this.condition()
        },
        async ban(id){
            let {message} = await ban(id)
            this.$message.success(message)
            this.condition()
        },
        async unban(id){
             let {message} = await unban(id)
            this.$message.success(message)
            this.condition()
        },
        handleSizeChange(val) {
        this.page.size  = val
        this.condition()
        },
        handleCurrentChange(val) {
            this.page.current = val
            this.condition()
        }
    },
    mounted() {
        this.condition()
    },  
}
</script>

<style>
 .line {
    text-align: center;
}
</style>